package org.infinitest.intellij;

import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.assertThat;
import org.infinitest.intellij.idea.language.IdeaInfinitestAnnotator;
import org.infinitest.TestEvent;
import static org.infinitest.TestEvent.testMethodError;
import static org.infinitest.TestEvent.testMethodFailed;
import static org.infinitest.TestEvent.testMethodSuccess;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;

@SuppressWarnings({"ThrowableInstanceNeverThrown"})
public class WhenAnnotatingTestEvents {
  private IdeaInfinitestAnnotator annotator;
  private TestEvent failure;
  private TestEvent success;
  private TestEvent error;

  @Before
  public void setUp() {
    annotator = new IdeaInfinitestAnnotator() {
      @Override
      protected void forceAnnotation() {
      }
    };
    error = testMethodError("anotherclass", new Exception());
    failure = testMethodFailed("message", "testName", "methodName", null);
    success = testMethodSuccess("testName", "methodName");
  }

  @Test
  public void shouldStoreFailure() {
    annotator.annotate(failure);
    assertThat(annotator.listStoredEvents(), hasItem(failure));
  }

  @Test
  public void shouldStoreMultipleEvents() {
    annotator.annotate(failure);
    annotator.annotate(error);
    assertThat(annotator.listStoredEvents(), hasItem(failure));
  }

  @Test
  public void shouldClearFailureOnSuccess() {
    annotator.annotate(failure);
    annotator.clearAnnotation(success);
    assertThat(annotator.listStoredEvents(), not(hasItem(failure)));
  }
}
